﻿using System;
using System.Collections.Generic;

using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using DTO;
namespace Website
{
    public partial class ajaxActions : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            string cmd = Request.QueryString["cmd"];
            if (cmd != null)
            {
                switch (cmd)
                {
                    case "Order":
                    {
                        try
                        {

                            int Id = int.Parse(Request.QueryString["ProductID"].ToString());
                            string Name = Request.QueryString["ProductName"].ToString();
                            float Quantity = float.Parse(Request.QueryString["Quantity"].ToString());
                            string Unit = Request.QueryString["Unit"].ToString();
                            float Price = float.Parse(Request.QueryString["Price"].ToString());

                            Basket basket = new Basket();
                            // if basket session null
                            if (Session["Basket"] != null)
                                basket = (Basket)Session["Basket"];

                            basket.addProduct(new Product(Id, Name, Quantity, Unit, Price));

                            Session["Basket"] = basket;
                            // view basket in html string - table format
                            Response.Write(basket.toHTMLString());
                        }
                        catch (Exception ex)
                        {
                            Response.Write("Lỗi addProduct --> " + ex.ToString());
                        }

                        break;
                    }
                    case "DelProduct":
                    {
                        try
                        {
                            int Id = int.Parse(Request.QueryString["ProductID"].ToString());
                           
                            Basket basket = new Basket();
                            // if basket session null
                            if (Session["Basket"] != null)
                                basket = (Basket)Session["Basket"];

                            basket.delProduct(Id);

                            Session["Basket"] = basket;
                            // view basket in html string - table format
                            Response.Write(basket.toHTMLString());
                        }
                        catch (Exception ex)
                        {
                            Response.Write("Lỗi DelProduct --> " + ex.ToString());
                        }

                        break;
                    }
                }
            }
        }
    }
}